﻿using System;
using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the item specification entity.
    /// </summary>
    public abstract class CatalogItemSpecEntity : AuditableEntity
    {
        #region Properties

        /// <summary>
        /// Gets the specification system code.
        /// </summary>
        public abstract string Code
        {
            get;
        }

        /// <summary>
        /// Gets or sets the item.
        /// </summary>
        public virtual CatalogItemEntity Item
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the SKU.
        /// </summary>
        public virtual string SKU
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the common flags.
        /// </summary>
        public virtual CatalogItemSpecFlags CommonFlags
        {
            get;
            set;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Converts the specification to the key-value pair collection.
        /// </summary>
        /// <returns>The key-value pair collection.</returns>
        public abstract IDictionary<String, String> ToDictionary();

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["Code"] = Code;
                obj["Item"] = Item.Disclose(DisclosureLevel.Primary, options);
                obj["SKU"] = SKU;
                obj["CommonFlags"] = CommonFlags.ToString();
            }
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class CatalogItemSpecMapping : EntityMapping<CatalogItemSpecEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public CatalogItemSpecMapping()
        {
            Table("fwk_CatalogItemSpecifications");
            Id(x => x.EntityID, m =>
            {
                m.Column("SpecificationID");
                m.Generator(Generators.Identity);
            });
            ManyToOne(x => x.Item, m =>
            {
                m.Column("ItemID");
                m.ForeignKey("FK_fwk_CatalogItemSpecifications_fwk_CatalogItems");
                m.NotNullable(true);
                m.Fetch(FetchKind.Join);
            });
            Property(x => x.SKU, m =>
            {
                m.NotNullable(true);
                m.Length(100);
                m.Column(c =>
                {
                    c.SqlType("nvarchar(100)");
                });
            });
            Property(x => x.CommonFlags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
        }
    }
}
